//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "ProviderBridge.h"
#include "StringUtilities.h"

//---------------------------------------------------------------------------

#pragma package(smart_init)

TProvider :: TProvider()
{
}

TProvider :: TProvider( TProvider* other)
{
   id   = other->id;
   name = other->name;
}

//-------------------------- Invoice Bridge ----------------------------------

TProviderBridge :: TProviderBridge( TDatabase * database)
{
   qryGet = new TQuery( NULL);
   qryGet->DatabaseName = database->DatabaseName;
   BuildGetQuery();

   qryAdd = new TQuery( NULL);
   qryAdd->DatabaseName = database->DatabaseName;
   BuildAddQuery();

   qryUpdate = new TQuery( NULL);
   qryUpdate->DatabaseName = database->DatabaseName;
   BuildUpdateQuery();

   qryDelete = new TQuery( NULL);
   qryDelete->DatabaseName = database->DatabaseName;
   BuildDeleteQuery();
               
   qryGetProviders = new TQuery( NULL);
   qryGetProviders->DatabaseName = database->DatabaseName;
   BuildGetProvidersQuery();

   qryInUse = new TQuery( NULL);
   qryInUse->DatabaseName = database->DatabaseName;
   BuildInUseQuery();
}

TProviderBridge :: ~TProviderBridge()
{
   delete qryInUse;
   delete qryGetProviders;
   delete qryDelete;
   delete qryUpdate;
   delete qryAdd;
   delete qryGet;
}

void TProviderBridge :: BuildGetQuery()
{
   qryGet->SQL->Add( "SELECT * FROM PROVIDERS");
   qryGet->SQL->Add( "WHERE (ID = :prmID)");
}

void TProviderBridge :: BuildAddQuery()
{
   qryAdd->SQL->Add( "INSERT INTO PROVIDERS (NAME, ADDRESS, PHONE) VALUES (:prmName, :prmAddress, :prmPhone)");
}

void TProviderBridge :: BuildUpdateQuery()
{
   qryUpdate->SQL->Add( "UPDATE PROVIDERS");
   qryUpdate->SQL->Add( "SET NAME = :prmName, ADDRESS = :prmAddress, PHONE = :prmPhone");
   qryUpdate->SQL->Add( "WHERE (ID = :prmID)");
}

void TProviderBridge :: BuildDeleteQuery()
{
   qryDelete->SQL->Add( "DELETE FROM PROVIDERS WHERE");
   qryDelete->SQL->Add( "(ID = :prmID)");
}

void TProviderBridge :: BuildGetProvidersQuery()
{
   qryGetProviders->SQL->Add( "SELECT * FROM PROVIDERS");
   qryGetProviders->SQL->Add( "ORDER BY NAME");
}

void TProviderBridge :: BuildInUseQuery()
{
   qryInUse->SQL->Add( "SELECT * FROM SHIPMENTS");
   qryInUse->SQL->Add( "WHERE PROVIDER_ID = :prmProviderID");
}

void SetProvider( TQuery* query, TProvider* provider)
{
   provider->id   = query->FieldByName( "ID")->AsInteger;
   provider->name = query->FieldByName( "NAME")->AsString;
   provider->address = query->FieldByName( "ADDRESS")->AsString;
   provider->phone = query->FieldByName( "PHONE")->AsString;
}

void TProviderBridge :: Get( __int64 id, TProvider* provider)
{
   qryGet->ParamByName( "prmID")->AsInteger = id;

   qryGet->Open();

   try
   {
      SetProvider( qryGet, provider);
   }
   __finally
   {
      qryGet->Close();
   }
}

void TProviderBridge :: GetProviders( TStrings * providers)
{
   TProvider * provider;
   
   qryGetProviders->Open();

   try
   {
      TStringUtilites :: Clear( providers);

      qryGetProviders->First();

      while ( !qryGetProviders->Eof)
      {
         provider = new TProvider();
         SetProvider( qryGetProviders, provider);

         providers->AddObject( provider->name, provider);
         qryGetProviders->Next();
      }
   }
   __finally
   {
      qryGetProviders->Close();
   }
}

void TProviderBridge :: Add( TProvider* provider)
{
   qryAdd->ParamByName( "prmName")->AsString = provider->name;
   qryAdd->ParamByName( "prmAddress")->AsString = provider->address;
   qryAdd->ParamByName( "prmPhone")->AsString = provider->phone;

   qryAdd->ExecSQL();
}

void TProviderBridge :: Update( TProvider* provider)
{
   qryUpdate->ParamByName( "prmID")->AsInteger = provider->id;
   qryUpdate->ParamByName( "prmName")->AsString = provider->name;
   qryUpdate->ParamByName( "prmAddress")->AsString = provider->address;
   qryUpdate->ParamByName( "prmPhone")->AsString = provider->phone;

   qryUpdate->ExecSQL();
}

void TProviderBridge :: Delete( __int64 id)
{
   qryDelete->ParamByName( "prmID")->AsInteger = id;
   qryDelete->ExecSQL();
}

bool TProviderBridge :: InUse( TProvider* provider)
{
   bool result = false;     
   qryInUse->ParamByName( "prmProviderID")->AsInteger = provider->id;

   qryInUse->Open();

   try
   {
      result = (!qryInUse->IsEmpty());
   }
   __finally
   {
      qryInUse->Close();
   }

   return result;
}
